package com.lai.note.interceptor;

import android.content.Context;
import android.util.Log;

import com.lai.note.model.User;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class AddCookiesInterceptor implements Interceptor {

    private Context context;

    public AddCookiesInterceptor(Context context){
        super();
        System.out.println("进来了-->  AddCookiesInterceptor");
        this.context = context;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        HashSet<String> preferences = (HashSet) context.getSharedPreferences("config",
                context.MODE_PRIVATE).getStringSet("cookie", null);
        System.out.println("preferences:--> "+preferences);
        if (preferences != null) {
            for (String cookie : preferences) {
                builder.addHeader("Cookie", cookie);
                System.out.println("cookie-->::"+cookie);
                Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I
                // know which headers are being added; this interceptor is used after the normal logging of OkHttp
            }
        }
        System.out.println("build's string is:--> "+builder.toString());
        return chain.proceed(builder.build());
    }
}
